home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / presto / prest_04.lha / src / objects.h < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-06  |  2.2 KB  |  115 lines

  1. class Oqueue;
  2.  
  3. //
  4. // Basic Object struct: Attack at will
  5. //    Objects expect to live in linked lists.  Whatever data
  6. //    actually needs to comprise the object lives in the derivation
  7. // Warning:  the offsets of fields in Thread and Process objects are dependent
  8. //    upon the length of the Object structure; some of these offsets are
  9. //    known to swtch.  If you change the length of Object, you must fix the
  10. //    offsets used in swtch.
  11. //
  12.  
  13. #define OBJ_ANY        0
  14. #define OBJ_THREAD    1
  15. #define OBJ_PROCESS    2
  16. #define OBJ_MONITOR    3
  17. #define OBJ_CONDVAR    4
  18. #define OBJ_LOCK    5
  19. #define OBJ_END        99        /* free to use after here */
  20.  
  21. struct Object    {
  22.     int     o_type;
  23.     char    *o_name;
  24.     Object    *o_next;
  25.     Object    *o_prev;        // unused in base impls
  26.     Object(int t=0, char* n=0)
  27.         { o_type = t; o_name = n; o_next = o_prev = 0;}
  28.     int& type()
  29.         { return o_type; }
  30.     char* name()
  31.         { return o_name; }
  32.     virtual void error(char* s);    // all objects must handle their own errors
  33.     virtual void print(ostream& = cout);
  34. };
  35. ostream& operator<<(ostream&s, Object*o);
  36.  
  37.  
  38.  
  39. struct Oqueue : public Object {
  40.     Object    *oq_head;
  41.     Object    *oq_tail;
  42.     int    oq_state;
  43.     Oqueue(Object* head = 0);
  44.     inline Object*    get();            // from head of queue
  45.     inline Object* lookat();        // return non-destructively
  46.     inline void append(Object* ol);        // to end of queue
  47.     inline void prepend(Object* ol);        // to head of queue
  48.     inline void remove(Object* ol);    // remove middle element
  49.     int&    state()
  50.         {return oq_state;}
  51.     int    empty()
  52.         {return oq_head == 0;}
  53.     virtual void print(ostream& = cout);
  54. };
  55.  
  56.  
  57. //
  58. // queue states
  59. //
  60.  
  61. #define OQ_FREE        0x00        // nothing going on
  62. #define OQ_BUSY        0x01        // someone's got us
  63. #define OQ_EMPTY    0x02        // nothing here
  64. #define OQ_ERROR    0x04        // something amiss
  65.  
  66.  
  67. inline
  68. Object*
  69. Oqueue::get()
  70. {
  71.     register Object *o = oq_head;
  72.     if (o)    {
  73.         oq_head = o->o_next;
  74.     }
  75.     return o;
  76. }
  77.  
  78. inline
  79. Object*
  80. Oqueue::lookat()
  81. {
  82.     return oq_head;
  83. }
  84.  
  85.  
  86. inline
  87. void
  88. Oqueue::append(register Object* o)
  89. {
  90.     if (oq_head)    {
  91.         oq_tail->o_next = o;
  92.         oq_tail = o;
  93.     } else
  94.         oq_head = oq_tail = o;
  95.     o->o_next = 0;
  96. }
  97.  
  98. inline
  99. void
  100. Oqueue::prepend(register Object* o)
  101. {
  102.     o->o_next = oq_head;
  103.     oq_head = o;
  104. }
  105.  
  106. inline
  107. void
  108. Oqueue::remove(register Object* o)
  109. {
  110.     o->error("Oqueue remove not implemented for single linked lists (yet)");
  111.     abort();
  112. }
  113.  
  114.  
  115.